#include <iostream>
#include <fcntl.h>    // 包含 O_RDONLY
#include <unistd.h>   // 包含 read, close
using namespace std;

int main() {
    const char* fifoPath = "./myfifo"; // 命名管道文件路径

    // 打开命名管道的读端
    int fd = open(fifoPath, O_RDONLY);
    if (fd == -1) {
        perror("open");
        return 1;
    }

    // 读取数据
    char buffer[128];
    ssize_t bytesRead = read(fd, buffer, sizeof(buffer) - 1);
    if (bytesRead > 0) {
        buffer[bytesRead] = '\0'; // 确保字符串以 '\0' 结尾
        cout << "Reader: Received message - " << buffer << endl;
    } else {
        perror("read");
    }

    // 关闭读端
    close(fd);

    // 删除命名管道文件
    unlink(fifoPath);
    return 0;
}
